<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button type="primary" @click="handleAdd">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-row :gutter="20">
                <el-col :span="24">
                    <div class="mt-4" v-loading="pager.loading">
                        <el-table :data="pager.lists" size="large">
                            <el-table-column prop="序号" type="index" min-width="60" />
                            <el-table-column prop="start_date" label="开始时间" min-width="100" />
                            <el-table-column prop="end_date" label="结束时间" min-width="100" />
                            <el-table-column label="操作" width="320" fixed="right">
                                <template #default="{ row }">
                                    <el-button link type="primary" @click="handleEdit(row)">
                                        编辑
                                    </el-button>
                                    <el-button link type="danger" @click="handleDelete(row.id)">
                                        删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="flex justify-end mt-4">
                            <pagination v-model="pager" @change="getLists" />
                        </div>
                    </div>
                </el-col>
                <!-- <el-col :span="12">
                    <div>
                        <CustomCalendar  />
                    </div>
                </el-col> -->
            </el-row>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

import { usePaging } from '@/hooks/usePaging'
import { calendarLists, calendarDelete } from '@/api/apis'
import feedback from '@/utils/feedback'
import CustomCalendar from '@/components/customCalendar/index.vue'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const { pager, getLists } = usePaging({
    fetchFun: calendarLists
})

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await calendarDelete({ id })
    getLists()
}
getLists()
</script>

<style scoped></style>